package com.xuecheng.basic.controller;

import com.xuecheng.basic.domain.Category;
import com.xuecheng.basic.service.CategoryService;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 课程分类前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService  categoryService;

    /**
     * 查询课程分类信息
     * 请求地址：GET /tree-nodes
     * 响应数据：ResponseResult<CategoryVo>
     */
    @GetMapping("/tree-nodes")
    public ResponseResult<CategoryVo> findTreeNodes() {
        return categoryService.findTreeNodes();
    }

    /**
     * 根据id查询分类数据
     * 地址：GET: /{id}
     * 返回：CategoryVo
     */
    @GetMapping("/{id}")
    public ResponseResult<CategoryVo> findById(@PathVariable("id") String id) {
        Category category = categoryService.getById(id);
        CategoryVo vo = BeanHelper.copyProperties(category, CategoryVo.class);
        return ResponseResult.okResult(vo);
    }
}
